package weaver.formmode.customjavacode.modeexpand.unionpay.market;

import com.customization.unionpay.market.service.ClearFundDetailsButtonService;
import com.customization.unionpay.market.service.impl.ClearFundDetailsButtonServiceImpl;
import com.customization.unionpay.market.util.FormDataUtils;
import com.customization.unionpay.market.util.PropUtils;
import com.engine.common.util.ServiceUtil;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sjm
 * @description 清算资金明细相关action
 */
public class ClearFundDetailsSave extends AbstractModeExpandJavaCodeNew {

    private static final Logger log = LoggerFactory.getLogger(ClearFundDetailsSave.class);

    private ClearFundDetailsButtonService getService(User user) {
        return ServiceUtil.getService(ClearFundDetailsButtonServiceImpl.class, user);
    }

    /**
     * 执行模块扩展动作
     *
     * @param param param包含(但不限于)以下数据
     *              user 当前用户
     *              importtype 导入方式(仅在批量导入的接口动作会传输) 1 追加，2覆盖,3更新，获取方式(int)param.get("importtype")
     *              导入链接中拼接的特殊参数(仅在批量导入的接口动作会传输)，比如a=1，可通过param.get("a")获取参数值
     *              页面链接拼接的参数，比如b=2,可以通过param.get("b")来获取参数
     * @return
     */
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        log.info("inClearFundDetailsSave");
        Map<String, String> result = new HashMap<>();
        try {
            User user = (User) param.get("user");
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo");
            if (requestInfo != null) {
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());

                Map<String, Object> allFormData = FormDataUtils.getAllFormData(requestInfo);

                Map<String, String>  mainMap = (Map<String, String>)allFormData.get("mainMap");

                if (billid > 0 && modeid > 0) {
                    //------请在下面编写业务逻辑代码------
                    // 【业务类型+资金来源+清算日期+数据来源】为唯一条件
                    /*
                    业务类型        ywlx
                    资金来源        zjly
                    清算日期        qsrq
                    数据来源        sjly
                    清算金额        qsje

                    业务类型        ywlx
                    资金来源        zjly
                    清算日期        qsrq
                    数据来源        sjly
                    清算金额合计     qsjehj
                    */

                    // 清算资金汇总台账表单id
                    int formId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "clearing_fund_summary_ledger_form_id"));
                    log.info("formId == >" +formId);
                    // 清算资金汇总台账模块id
                    int formmodeId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "clearing_fund_summary_ledger_formmode_id"));
                    log.info("formmodeId == >" +formmodeId);

                    String ywlx = mainMap.get("ywlx");
                    String zjly = mainMap.get("zjly");
                    String qsrq = mainMap.get("qsrq");
                    String sjly = mainMap.get("sjly");
                    String qsje = mainMap.get("qsje");

                    getService(user).addOrUpdateInfo(formId,formmodeId,user,ywlx,zjly,qsrq,sjly,qsje);

                }
            }
        } catch (Exception e) {
            log.error("saveClearAll ：" + e.getMessage());
            result.put("errmsg", "自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }

}
